﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using CP.Entities;
using CP.Controllers.GPM;
using DevExpress.XtraEditors;
using CP.Utility;

namespace GPM
{
    public partial class FrmEditDepartment : BaseEditForm
    {
        DepartmentController controller = new DepartmentController();
        public int DeptID { get; private set; }
        public FrmEditDepartment()
        {
            InitializeComponent();
        }
        public FrmEditDepartment(string id, int deptid)
            : this()
        {
            base.EntityID = id;
            this.DeptID = deptid;
        }
        private void SetInfo(Department dept)
        {
            dept.dept_id = Convert.ToInt32(this.txtDeptID.Text);
            dept.dept_name = this.txtDeptName.Text; 
        }

        /// <summary>
        /// 数据字典加载
        /// </summary>
        private void InitDictItem()
        {
            
        }

        public override bool CheckInput()
        {
            if (string.IsNullOrEmpty(this.txtDeptID.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：部门ID不能为空！", AppSetter.SoftName);
                return false;
            }

            if (string.IsNullOrEmpty(this.txtDeptName.Text.Trim()))
            {
                ExMessageBox.Show("提示信息：部门名称不能为空！", AppSetter.SoftName);
                return false;
            }

            return true;
        }

        public override void DisplayData()
        {
            InitDictItem();

            if (!string.IsNullOrEmpty(EntityID))
            {
                Department info = controller.GetDeptByID(this.DeptID);
                #region 显示用户信息

                this.txtDeptID.Focus();
                if (null != info)
                {
                    this.txtDeptID.Text = info.dept_id.ToString() ;
                    this.txtDeptID.Enabled = false;
                    this.txtDeptName.Text = info.dept_name; 
                }

                #endregion
            }
            else
            {
                this.txtDeptID.Focus();
            };
        }

        public override bool SaveAddNew()
        {
            Department dept = new Department();
            this.SetInfo(dept);
            controller.AddDept(dept);
            (this.Owner.ActiveMdiChild as FrmDepartment).RefreshDept();
            return true;
        }

        public override bool SaveUpdated()
        {
            Department dept = new Department();
            this.SetInfo(dept);
            controller.EditDept(dept);
            (this.Owner.ActiveMdiChild as FrmDepartment).RefreshDept();
            return true;
        }
    }
}
